<template>
  <div class="monitor-container">
    <!-- 统计卡片 -->
    <div class="stats-cards">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon running">
                <el-icon><Operation /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-value">{{ stats.runningTasks }}</div>
                <div class="stat-label">运行中任务</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon pending">
                <el-icon><Clock /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-value">{{ stats.pendingTasks }}</div>
                <div class="stat-label">待执行任务</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon completed">
                <el-icon><Check /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-value">{{ stats.completedTasks }}</div>
                <div class="stat-label">已完成任务</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon error">
                <el-icon><Warning /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-value">{{ stats.errorTasks }}</div>
                <div class="stat-label">异常任务</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 搜索表单 -->
    <el-form :model="searchForm" ref="searchFormRef" :inline="true" class="search-form">
      <el-form-item label="任务编号" prop="taskNo">
        <el-input v-model="searchForm.taskNo" placeholder="请输入任务编号" clearable />
      </el-form-item>
      <el-form-item label="产线" prop="productionLineId">
        <el-select v-model="searchForm.productionLineId" placeholder="请选择产线" clearable style="width: 180px">
          <el-option label="产线A" value="1" />
          <el-option label="产线B" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="任务状态" prop="status">
        <el-select v-model="searchForm.status" placeholder="请选择状态" clearable style="width: 180px">
          <el-option label="运行中" value="RUNNING" />
          <el-option label="待执行" value="PENDING" />
          <el-option label="已完成" value="COMPLETED" />
          <el-option label="异常" value="ERROR" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
        <el-button type="success" :icon="Refresh" @click="handleRefresh">刷新</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" style="width: 100%" v-loading="loading" border>
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="taskNo" label="任务编号" />
      <el-table-column prop="workOrderNo" label="工单编号" />
      <el-table-column prop="productName" label="产品名称" />
      <el-table-column prop="productionLineName" label="执行产线" />
      <el-table-column prop="currentStep" label="当前工序" />
      <el-table-column prop="progress" label="进度">
        <template #default="scope">
          <el-progress :percentage="scope.row.progress" :color="getProgressColor(scope.row.progress)" />
        </template>
      </el-table-column>
      <el-table-column prop="status" label="任务状态">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">{{ getStatusText(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="startTime" label="开始时间" />
      <el-table-column prop="estimatedEndTime" label="预计完成时间" />
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="scope">
          <el-button type="primary" link :icon="View" @click="handleView(scope.row)">查看详情</el-button>
          <el-button type="warning" link :icon="VideoPause" @click="handlePause(scope.row)" v-if="scope.row.status === 'RUNNING'">暂停</el-button>
          <el-button type="success" link :icon="VideoPlay" @click="handleResume(scope.row)" v-if="scope.row.status === 'PAUSED'">恢复</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        :current-page="pagination.currentPage"
        :page-size="pagination.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
      />
    </div>
  </div>
</template>

<script setup lang="ts" name="taskMonitor">
import { ref, reactive, onMounted } from "vue";
import type { FormInstance } from "element-plus";
import { ElMessage } from "element-plus";
import { View, VideoPause, VideoPlay, Refresh, Operation, Clock, Check, Warning } from "@element-plus/icons-vue";

const loading = ref(false);
const tableData = ref([]);
const searchFormRef = ref<FormInstance>();

// 统计数据
const stats = reactive({
  runningTasks: 12,
  pendingTasks: 8,
  completedTasks: 45,
  errorTasks: 2
});

// 查询表单
const searchForm = reactive({
  taskNo: "",
  productionLineId: "",
  status: ""
});

// 分页配置
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
});

// 获取进度条颜色
const getProgressColor = (progress: number) => {
  if (progress < 30) return '#f56c6c';
  if (progress < 70) return '#e6a23c';
  return '#67c23a';
};

// 获取状态类型
const getStatusType = (status: string) => {
  const statusMap: Record<string, string> = {
    RUNNING: "success",
    PENDING: "warning",
    COMPLETED: "info",
    ERROR: "danger",
    PAUSED: "warning"
  };
  return statusMap[status] || "info";
};

// 获取状态文本
const getStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    RUNNING: "运行中",
    PENDING: "待执行",
    COMPLETED: "已完成",
    ERROR: "异常",
    PAUSED: "已暂停"
  };
  return statusMap[status] || status;
};

// 获取表格数据
const getTableData = async () => {
  loading.value = true;
  try {
    // TODO: 调用API获取生产任务监控数据
    console.log("获取生产任务监控数据");
  } catch (error) {
    console.error("获取生产任务监控数据失败:", error);
    ElMessage.error("获取生产任务监控数据失败");
  } finally {
    loading.value = false;
  }
};

// 查询
const handleSearch = () => {
  pagination.currentPage = 1;
  getTableData();
};

// 重置
const handleReset = () => {
  if (!searchFormRef.value) return;
  searchFormRef.value.resetFields();
  pagination.currentPage = 1;
  getTableData();
};

// 刷新
const handleRefresh = () => {
  getTableData();
  ElMessage.success("数据已刷新");
};

// 分页大小改变
const handleSizeChange = (val: number) => {
  pagination.pageSize = val;
  getTableData();
};

// 当前页改变
const handleCurrentChange = (val: number) => {
  pagination.currentPage = val;
  getTableData();
};

// 查看详情
const handleView = (row: any) => {
  ElMessage.info("查看任务详情功能待实现");
};

// 暂停任务
const handlePause = (row: any) => {
  ElMessage.info("暂停任务功能待实现");
};

// 恢复任务
const handleResume = (row: any) => {
  ElMessage.info("恢复任务功能待实现");
};

// 页面加载时获取数据
onMounted(() => {
  getTableData();
});
</script>

<style scoped>
.monitor-container {
  padding: 20px;
}

.stats-cards {
  margin-bottom: 20px;
}

.stat-card {
  border-radius: 8px;
}

.stat-content {
  display: flex;
  align-items: center;
  padding: 10px;
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
  font-size: 24px;
  color: white;
}

.stat-icon.running {
  background: linear-gradient(135deg, #67c23a, #85ce61);
}

.stat-icon.pending {
  background: linear-gradient(135deg, #e6a23c, #f0c78a);
}

.stat-icon.completed {
  background: linear-gradient(135deg, #409eff, #79bbff);
}

.stat-icon.error {
  background: linear-gradient(135deg, #f56c6c, #f89898);
}

.stat-info {
  flex: 1;
}

.stat-value {
  font-size: 28px;
  font-weight: bold;
  color: #303133;
  line-height: 1;
}

.stat-label {
  font-size: 14px;
  color: #909399;
  margin-top: 5px;
}

.search-form {
  background-color: #fff;
  padding: 20px;
  border-radius: 4px;
  margin-bottom: 16px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  flex-shrink: 0;
}
</style>